package org.kernely.ui.admin;

import java.util.ArrayList;

import org.kernely.security.persistence.Role;
import org.kernely.ui.KernelyUIBundleDetectionEvent;
import org.kernely.ui.MainApplication;
import org.kernely.ui.mvp.IKernelyPresenter;
import org.kernely.ui.mvp.IKernelyView;
import org.kernely.ui.mvp.KernelyEvent;
import org.kernely.ui.mvp.KernelyEventReceiver;
import org.kernely.ui.mvp.KernelyPresenter;
import org.kernely.ui.services.KernelyUIBundle;

import com.vaadin.terminal.Resource;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Layout;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Window.Notification;
import com.vaadin.ui.themes.BaseTheme;
import com.vaadin.ui.themes.Reindeer;
/**
 *	Administration presenter of Kernely, where bundles with administration panel are displayed.
 */
public class KernelyAdminPresenter extends KernelyPresenter<KernelyAdminView> implements KernelyEventReceiver{

	private static final long serialVersionUID = 5148986719817951917L;

	/**
	 * @see org.kernely.core.service.IKernelyPresenter#bind()
	 */
	@Override
	public void bind() {
		// Registering as an event receiver to be called by KernelyProfileAttachedEvent
		MainApplication.getEventBus().addListener(this);
	}

	private void displayBundlesList(){
		if(!MainApplication.getSecurityService().getCurrentUser().hasOneOf(Role.ROLE_ADMINISTRATOR)){
			view.getWindow().showNotification("Error", "You are not an administrator !", Notification.TYPE_ERROR_MESSAGE);
		}
		else{

			this.view.getBundlesLayout().removeAllComponents();

			view.getBundleAdminPageLabel().setValue("Here you can administrate installed bundles. Click on the icon of the bundle you want to administrate to open it's administration panel.");
			view.getBundleAdminPageLabel().removeStyleName(Reindeer.LABEL_H2);
			view.getBundleAdminPageLayoutContent().removeAllComponents();

			// General administration panel
			final Resource mainResource = new ThemeResource("img/admin32.png");
			Button button = new Button();
			button.setStyleName(BaseTheme.BUTTON_LINK);
			button.setIcon(mainResource);
			button.setCaption("Main admin");
			button.addListener(new Button.ClickListener() {
				/**
				 * Action when the button is clicked
				 */
				@Override
				public void buttonClick(ClickEvent event) {
					view.getBundleAdminPageLabel().setValue("Main admin");
					view.getBundleAdminPageLabel().setIcon(mainResource);
					view.getBundleAdminPageLabel().setStyleName(Reindeer.LABEL_H2);

					// Retrieve view, presenter, and associate them
					IKernelyPresenter adminPresenter = new KernelyAdministrationPagePresenter();
					IKernelyView adminView = new KernelyAdministrationPageView();
					adminPresenter.setView(adminView);

					view.getBundleAdminPageLayoutContent().removeAllComponents();
					view.getBundleAdminPageLayoutContent().addComponent((Layout)adminPresenter.getView());
				}
			});
			button.setSizeUndefined();

			this.view.getBundlesLayout().addComponent(button);



			ArrayList<KernelyUIBundle> bundlesList = new ArrayList<KernelyUIBundle>(MainApplication.getKernelyUIBundleService().getUIBundles());
			for (KernelyUIBundle b : bundlesList){
				final KernelyUIBundle bundle = b;
				// Verify that the bundle got an administration view
				Class<? extends IKernelyView> adminView = bundle.getAdministrationView();
				if(adminView != null){
					Resource bundlePicture = bundle.getIcon();

					// If the bundle don't have a picture, we display a default one.
					if (bundlePicture == null){
						bundlePicture = new ThemeResource("img/default_bundle.png");
					}
					final Resource bundlePictureFinal = bundlePicture;
					Button bundleButton = new Button();
					bundleButton.setStyleName(BaseTheme.BUTTON_LINK);
					bundleButton.setIcon(bundlePicture);
					bundleButton.setCaption(bundle.getName());
					bundleButton.addListener(new Button.ClickListener() {
						/**
						 * Action when the button is clicked
						 */
						@Override
						public void buttonClick(ClickEvent event) {
							view.getBundleAdminPageLabel().setValue(bundle.getName());
							view.getBundleAdminPageLabel().setIcon(bundlePictureFinal);
							view.getBundleAdminPageLabel().setStyleName(Reindeer.LABEL_H2);
							try {
								// Retrieve view, presenter, and associate them
								IKernelyPresenter BAPresenter = (IKernelyPresenter) bundle.getViewPresenter().get(bundle.getAdministrationView()).newInstance();
								IKernelyView BAView = bundle.getAdministrationView().newInstance();
								BAPresenter.setView(BAView);

								view.getBundleAdminPageLayoutContent().removeAllComponents();
								view.getBundleAdminPageLayoutContent().addComponent((Layout)BAPresenter.getView());
							} catch (InstantiationException e) {
								//							MainApplication.getLogger().log(LogService.LOG_ERROR, "Fail to instanciate " + bundle.getName() + " administration panel...");
							} catch (IllegalAccessException e) {
								//							MainApplication.getLogger().log(LogService.LOG_ERROR, "Fail to instanciate " + bundle.getName() + " administration panel...");
							}
						}
					});
					bundleButton.setSizeUndefined();

					this.view.getBundlesLayout().addComponent(bundleButton);
				}
			}
		}
	}

	/**
	 * @see org.kernely.core.service.KernelyEventReceiver#handleViewEvent(KernelyEvent)
	 */
	@Override
	public final void handleKernelyEvent(KernelyEvent event) {
		// When the KernelyAdminView is attached to the application (i.e. when it is added as a component), we can set images.
		if (event instanceof KernelyAdminAttachedEvent){
			displayBundlesList();
		}
		//When a bundle is added or removed, we refresh page
		if (event instanceof KernelyUIBundleDetectionEvent){
			displayBundlesList();
		}
	}

	/**
	 * Called when the presenter is no more visible.
	 */
	@Override
	public final void unset() {
		MainApplication.getEventBus().removeListener(this);
	}
}
